package com.dodock.banglapapers.server.helper;

import java.io.IOException;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Text;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

public class DDNNewsDetailBanglanews24 {

	private String BASE_URL = "http://www.banglanews24.com/";

	public String Generate(String newsid) throws IOException {
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		try {
			Entity detailsEntity = datastore.get(KeyFactory.createKey("NewsDetails", "banglanews24__"+newsid));
			Text text = (Text)detailsEntity.getProperty("data");
			String data = Settings.replaceExtras(text.getValue());
			return data;
		} catch (EntityNotFoundException e) {
			String url = "";
			boolean isLifeStyleNews = false;
			if(newsid.startsWith("____")){
				isLifeStyleNews = true;
				url = "http://www.banglanews24.com/LifeStyle/" + newsid.substring(4);
			}else{
				url = BASE_URL + newsid;
			}
			
			Connection.Response response = Jsoup.connect(url)
		            .userAgent("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.21 (KHTML, like Gecko) Chrome/19.0.1042.0 Safari/535.21")
		            .timeout(10000)
		            .execute();
			
			Document doc = response.parse();
			String data = "{}";
			JsonObject news = new JsonObject();
			news.addProperty("id", newsid);
			news.addProperty("sharelink", url);
			if(isLifeStyleNews){
				data = parseLifeStyleNewsDetails(doc, news, datastore, newsid);
			}else{
				data = parseNews(doc, news, datastore, newsid);
			}
			return data;
		}
	}
	
	private String parseLifeStyleNewsDetails(Document doc, JsonObject news, DatastoreService datastore, String newsid) {		
		Element detailElement = doc.select("div[id=content_middle]").first();
		if(detailElement != null){
			 Element imgElement = detailElement.select("img").first();
			 if(imgElement != null){
				 String image = imgElement.attr("src");
				 JsonArray imageArray = new JsonArray();
				 JsonObject obj = new JsonObject();
				 obj.addProperty("url", image);
				 System.out.println("lifestyle detail img : "+image);
				 imageArray.add(obj);
				 image = imgElement.attr("src");
				 news.add("gallery", imageArray);
			 }
			
			 Element detailsTextElements = detailElement.select("div[style=margin-top:30px;padding-top:30px;]").first();
			 String details = "";
			 Elements elements = detailsTextElements.select("p");
				for (Element element : elements) {
					/*if(element.select("span[style=color: #888888;]").isEmpty()){
//						element = element.before("span[style=color: #888888;]");
					}*/
					details += element.text().replaceAll("\u00a0", "<br/>");
				}
				news.addProperty("details", details);
				
				String data = news.toString();
				data = Settings.replaceExtras(data);
				
				Text text = new Text(data);
				Entity detailsEntity = new Entity("NewsDetails", "banglanews24__"+newsid);
				detailsEntity.setProperty("data", text);
				datastore.put(detailsEntity);
				
				return data;
			} else{
				return "{}";
			}
	}

	private String parseNews(Document doc , JsonObject news, DatastoreService datastore, String newsid){
		Element newsDetailElement = doc.select("table[class=TblDetailsNewsBody]").first();
		if (newsDetailElement != null) {
			
			Elements detailsElements = newsDetailElement.select("div[class=DContent]");
			for (Element detailElement : detailsElements) {
				Elements pShowTextElement = detailElement.select("p[class=pShowText]");
				if(!pShowTextElement.isEmpty()){
					String details = "";
					String image = "";
					Elements imgElements = detailElement.select("img");
					if(!imgElements.isEmpty()){
						Element imgElement = imgElements.first();
						image = imgElement.attr("src");
						JsonArray imageArray = new JsonArray();
						JsonObject obj = new JsonObject();
						
						obj.addProperty("url", image);
						imageArray.add(obj);
						image = imgElement.attr("src");
						news.add("gallery", imageArray);
					}
					
					Elements elements = detailElement.select("p");
					for (Element element : elements) {
						details += element.text().replaceAll("\u00a0", "<br/>");
					}
					news.addProperty("details", details);
					break;
				}
			}
			
			String data = news.toString();
			data = Settings.replaceExtras(data);
			
			Text text = new Text(data);
			Entity detailsEntity = new Entity("NewsDetails", "banglanews24__"+newsid);
			detailsEntity.setProperty("data", text);
			datastore.put(detailsEntity);
			
			return data;
		} else{
			return "{}";
		}
	}

}
